﻿Imports System.Data.OleDb

Public Class frmThemSanPhamTheoNhom
    Public NhomId As Integer = -1
    Private Sub frmThemSanPhamTheoNhom_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.BackColor = Color.FromArgb(194, 217, 247)
            Nhom_Load()
            cbxNhom.SelectedValue = NhomId
            ''
            Dim dtNhom As New DataTable
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim adapter As OleDb.OleDbDataAdapter
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            dbCommand = New OleDbCommand("spSanPhamNhom_GetAll", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.Add("@NhomId", OleDbType.Integer).Value = cbxNhom.SelectedValue
            adapter = New OleDb.OleDbDataAdapter(dbCommand)
            adapter.Fill(dtNhom)
            dbConn.Close()
            Dim ListIDSanPhamSelected As String = ""
            For i As Integer = 0 To dtNhom.Rows.Count - 1
                ListIDSanPhamSelected = ListIDSanPhamSelected & "," & dtNhom.Rows(i)("SanPhamId")
            Next
            If ListIDSanPhamSelected.StartsWith(",") Then
                ListIDSanPhamSelected = ListIDSanPhamSelected.Remove(0, 1)
            ElseIf ListIDSanPhamSelected.EndsWith(",") Then
                ListIDSanPhamSelected = ListIDSanPhamSelected.Remove(ListIDSanPhamSelected.Length - 1, 1)
            End If
            lblListIDSanPhamSelected.Text = ListIDSanPhamSelected
            ''
            SanPham_Search(txtMaSanPham.Text.Trim, lblListIDSanPhamSelected.Text.Trim)
            SanPham_LoadRight(txtMaSanPham.Text.Trim, lblListIDSanPhamSelected.Text.Trim)
        Catch ex As Exception

        End Try
    End Sub
#Region "Load Data"
    Protected Sub Nhom_Load()
        Try
            Dim dtNhom As New DataTable
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim adapter As OleDb.OleDbDataAdapter
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            dbCommand = New OleDbCommand("spNhom_GetAll", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            adapter = New OleDb.OleDbDataAdapter(dbCommand)
            adapter.Fill(dtNhom)
            dbConn.Close()
            cbxNhom.DataSource = dtNhom
            cbxNhom.ValueMember = "NhomId"
            cbxNhom.DisplayMember = "TenNhom"
        Catch ex As Exception

        End Try
    End Sub
    Protected Sub SanPham_Search(ByVal MaSanPham As String, ByVal ListIdSelected As String)
        Try
            Dim dtSanPham As New DataTable
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim adapter As OleDb.OleDbDataAdapter
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            dbCommand = New OleDbCommand("spSanPham_Search", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.Add("@MaSanPham", OleDbType.VarWChar).Value = MaSanPham
            dbCommand.Parameters.Add("@ListIdSelected", OleDbType.VarWChar).Value = ListIdSelected
            dbCommand.Parameters.Add("@Type", OleDbType.VarWChar).Value = "NotIn"
            adapter = New OleDb.OleDbDataAdapter(dbCommand)
            adapter.Fill(dtSanPham)
            dbConn.Close()
            dgvSanPhamLeft.AutoGenerateColumns = False
            dgvSanPhamLeft.DataSource = dtSanPham
        Catch ex As Exception

        End Try
    End Sub
    Protected Sub SanPham_LoadRight(ByVal MaSanPham As String, ByVal ListIdSelected As String)
        Try
            Dim dtSanPham As New DataTable
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim adapter As OleDb.OleDbDataAdapter
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            dbCommand = New OleDbCommand("spSanPham_Search", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.Add("@MaSanPham", OleDbType.VarWChar).Value = MaSanPham
            dbCommand.Parameters.Add("@ListIdSelected", OleDbType.VarWChar).Value = ListIdSelected
            dbCommand.Parameters.Add("@Type", OleDbType.VarWChar).Value = "In"
            adapter = New OleDb.OleDbDataAdapter(dbCommand)
            adapter.Fill(dtSanPham)
            dbConn.Close()
            dgvSanPhamRight.AutoGenerateColumns = False
            dgvSanPhamRight.DataSource = dtSanPham
        Catch ex As Exception

        End Try
    End Sub
#End Region


    Private Sub txtMaSanPham_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMaSanPham.TextChanged
        Try
            SanPham_Search(txtMaSanPham.Text.Trim, lblListIDSanPhamSelected.Text.Trim)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub dgvSanPhamLeft_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvSanPhamLeft.CellMouseClick
        Try

        Catch ex As Exception

        End Try
    End Sub

    Private Sub dgvSanPhamLeft_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvSanPhamLeft.CellMouseUp
        Try
            If e.ColumnIndex = 0 Then
                dgvSanPhamLeft.EndEdit()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub dgvSanPhamLeft_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSanPhamLeft.CellValueChanged
        Try
            If e.ColumnIndex = 0 Then
                Dim check As DataGridViewCheckBoxCell
                check = CType(dgvSanPhamLeft("Chon", e.RowIndex), DataGridViewCheckBoxCell)
                If check.Value = "0" Then
                    dgvSanPhamLeft.CurrentRow.DefaultCellStyle.BackColor = Color.White
                ElseIf check.Value = "1" Then
                    dgvSanPhamLeft.CurrentRow.DefaultCellStyle.BackColor = Color.Aquamarine
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub


    Private Sub cmdChon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChon.Click
        Try
            Dim ListIDSanPhamSelected As String = lblListIDSanPhamSelected.Text.Trim
            For i As Integer = 0 To dgvSanPhamLeft.RowCount - 1
                If dgvSanPhamLeft(0, i).Value = "1" Then
                    ListIDSanPhamSelected = ListIDSanPhamSelected & "," & dgvSanPhamLeft("IDSanPham", i).Value
                End If
            Next
            If ListIDSanPhamSelected.StartsWith(",") Then
                ListIDSanPhamSelected = ListIDSanPhamSelected.Remove(0, 1)
            ElseIf ListIDSanPhamSelected.EndsWith(",") Then
                ListIDSanPhamSelected = ListIDSanPhamSelected.Remove(ListIDSanPhamSelected.Length - 1, 1)
            End If
            'Load Left
            lblListIDSanPhamSelected.Text = ListIDSanPhamSelected
            SanPham_Search(txtMaSanPham.Text.Trim, lblListIDSanPhamSelected.Text.Trim)
            SanPham_LoadRight("", lblListIDSanPhamSelected.Text.Trim)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdBoChon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBoChon.Click
        Try
            Dim ListIDSanPhamSelected As String = lblListIDSanPhamSelected.Text.Trim
            For i As Integer = 0 To dgvSanPhamRight.RowCount - 1
                If dgvSanPhamRight(0, i).Value = "1" Then
                    ListIDSanPhamSelected = ListIDSanPhamSelected.Replace(dgvSanPhamRight("IDSanPhamR", i).Value, "")
                End If
            Next
            ListIDSanPhamSelected = ListIDSanPhamSelected.Replace(",,", ",")

            If ListIDSanPhamSelected.StartsWith(",") Then
                ListIDSanPhamSelected = ListIDSanPhamSelected.Remove(0, 1)
            ElseIf ListIDSanPhamSelected.EndsWith(",") Then
                ListIDSanPhamSelected = ListIDSanPhamSelected.Remove(ListIDSanPhamSelected.Length - 1, 1)
            End If
            'Load Left
            lblListIDSanPhamSelected.Text = ListIDSanPhamSelected
            SanPham_Search(txtMaSanPham.Text.Trim, lblListIDSanPhamSelected.Text.Trim)
            SanPham_LoadRight("", lblListIDSanPhamSelected.Text.Trim)
        Catch ex As Exception

        End Try
    End Sub


    Private Sub cmdLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLuu.Click
        Try
            'Kiểm Tra Chỉ Tồn Tại Một Giá Duy Nhất
            Dim DonGia As String
            Dim DonGia2 As String
            If dgvSanPhamRight.RowCount > 0 Then
                DonGia = dgvSanPhamRight("DonGiaR", 0).Value
                For i As Integer = 1 To dgvSanPhamRight.RowCount - 1
                    DonGia2 = dgvSanPhamRight("DonGiaR", i).Value
                    If DonGia2 <> DonGia Then
                        MessageBox.Show("Sản phẩm trong nhóm không hợp lệ vì tồn tại các sản phẩm có giá khác nhau.Vui lòng kiểm tra lại!", "Thông báo", MessageBoxButtons.OK)
                        ''set focus row
                        dgvSanPhamRight.Rows(i).DefaultCellStyle.BackColor = Color.Aquamarine
                        dgvSanPhamRight.FirstDisplayedScrollingRowIndex = i
                        Exit Sub
                    End If
                Next
                Dim dbConn As New OleDbConnection
                Dim dataAccess As New DataAccess
                Dim dbCommand As OleDbCommand
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                dbCommand = New OleDbCommand("SanPhamNhom_Add", dbConn)
                dbCommand.CommandType = CommandType.StoredProcedure
                dbCommand.Parameters.Add("@NhomId", OleDbType.Integer).Value = cbxNhom.SelectedValue
                dbCommand.Parameters.Add("@ListSanPhamId", OleDbType.VarWChar).Value = lblListIDSanPhamSelected.Text.Trim
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
            End If
            Me.Close()
            txtMaSanPham.Text = ""
            lblListIDSanPhamSelected.Text = ""
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdDong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDong.Click
        Try
            txtMaSanPham.Text = ""
            lblListIDSanPhamSelected.Text = ""
            Me.Close()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cbxNhom_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxNhom.SelectionChangeCommitted
        Try
            Dim dtNhom As New DataTable
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim adapter As OleDb.OleDbDataAdapter
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            dbCommand = New OleDbCommand("spSanPhamNhom_GetAll", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.Add("@NhomId", OleDbType.Integer).Value = cbxNhom.SelectedValue
            adapter = New OleDb.OleDbDataAdapter(dbCommand)
            adapter.Fill(dtNhom)
            dbConn.Close()
            Dim ListIDSanPhamSelected As String = ""
            For i As Integer = 0 To dtNhom.Rows.Count - 1
                ListIDSanPhamSelected = ListIDSanPhamSelected & "," & dtNhom.Rows(i)("SanPhamId")
            Next
            If ListIDSanPhamSelected.StartsWith(",") Then
                ListIDSanPhamSelected = ListIDSanPhamSelected.Remove(0, 1)
            ElseIf ListIDSanPhamSelected.EndsWith(",") Then
                ListIDSanPhamSelected = ListIDSanPhamSelected.Remove(ListIDSanPhamSelected.Length - 1, 1)
            End If
            lblListIDSanPhamSelected.Text = ListIDSanPhamSelected
            ''
            SanPham_Search(txtMaSanPham.Text.Trim, lblListIDSanPhamSelected.Text.Trim)
            SanPham_LoadRight(txtMaSanPham.Text.Trim, lblListIDSanPhamSelected.Text.Trim)
        Catch ex As Exception

        End Try
    End Sub
End Class